package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dal.entitys.Address;
import com.cskaoyan.shopping.dal.entitys.PromoSession;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * \* User: 余雨健
 * \* Date: 2022/10/25
 * \* Time: 16:38
 **/
@RestController
public class ShoppingShowController {

    @Autowired
    IHomeService iHomeService;
    @Autowired
    IContentService iContentService;
    @Autowired
    IProductCateService iProductCateService;
    @Autowired
    IProductService iProductService;
    @Autowired
    IPromoSessionService iPromoSessionService;
    @Autowired
    IUserAddressService iUserAddressService;

    /**
     *@首页显示
     *@author yuyujian
     *@Date 2022/10/26
     *@Time 11:47
     */
    @GetMapping("/shopping/homepage")
    public ResponseData GetShoppingHomePage(){
        Set<PanelDto> homepage = iHomeService.homepage();
        ResponseData responseData = new ResponseUtil().setData(homepage);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }

    /**
     *@首页导航栏显示
     *@author yuyujian
     *@Date 2022/10/26
     *@Time 11:47
     */
    @GetMapping("/shopping/navigation")
    public ResponseData GetShoppingNavigation(){
        List<PanelContentDto> panelContentDtos = iContentService.queryNavList();
        ResponseData responseData = new ResponseUtil().setData(panelContentDtos);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }

    /**
     *@列举所有商品种类
     *@author yuyujian
     *@Date 2022/10/26
     *@Time 11:48
     */
    @GetMapping("/shopping/categories")
    public ResponseData GetShoppingCategories(){
        String sort = "1";
        List<ProductCateDto> allProductCate = iProductCateService.getAllProductCate(sort);
        ResponseData responseData = new ResponseUtil().setData(allProductCate);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }
    /**
     *@商品详情
     *@author yuyujian
     *@Date 2022/10/26
     *@Time 14:08
     */
    @GetMapping("/shopping/product/*")
    public ResponseData GetShoppingProduct(HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String[] split = requestURI.split("/");
        long id = Long.parseLong(split[3]);
        ProductDetailDto productDetail = iProductService.getProductDetail(id);
        ResponseData responseData = new ResponseUtil().setData(productDetail);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }

    /**
     *@秒杀活动--不知道返回参数类型
     *@author yuyujian
     *@Date 2022/10/27
     *@Time 14:21
     */
    @GetMapping("/shopping/seckilllist")
    public ResponseData seckilllist(Integer sessionId){
        PromoSession seckilllist = iPromoSessionService.Seckilllist(sessionId);
        if(seckilllist == null){
            return new ResponseUtil<>().setErrorMsg("秒杀活动不存在");
        }
        ResponseData responseData = new ResponseUtil().setData(seckilllist);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }

    /**
     *@显示已保存的地址
     *@author yuyujian
     *@Date 2022/10/27
     *@Time 14:51
     */
    @GetMapping("/shopping/addresses")
    public ResponseData address(HttpServletRequest request){
        //获取名为access_token的Cookies并解析
        String access_token = CookieUtil.getCookieValue(request, "access_token");
        //判断token是否存在
        if(access_token == null){
            return new ResponseUtil().setErrorMsg("系统参数错误，请重新登陆");
        }
        JwtTokenUtils freeJwtTool = JwtTokenUtils.builder().token(access_token).build();
        String username = freeJwtTool.freeJwt();
        List<Address> addresses = iUserAddressService.userAddress(username);
        ResponseData responseData = new ResponseUtil().setData(addresses);
        long l = System.currentTimeMillis();
        responseData.setTimestamp(l);
        return responseData;
    }
}
